package com.libbase.mvp.presenter;


import com.base.kotlin.mvp.BasePresenter;

import java.util.Objects;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class NewPresenter extends BasePresenter<NewContract.View, NewContract.Model> implements NewContract.Presenter {

    @Override
    protected NewContract.Model getModel() {
        return new NewModel();
    }

    @Override
    public void getNews(String nav) {
        if (isViewAttached()) {
            Objects.requireNonNull(getMView()).showLoading();
            try {
                Disposable disposable = Objects.requireNonNull(getMModel()).getNews(nav).subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread()).subscribe(result -> {
                            getMView().hideLoading();
                            getMView().onSuccessNews(result);
                        });
                getMDisposables().add(disposable);
            } catch (RuntimeException e) {
                Objects.requireNonNull(getMView()).hideLoading();
                Objects.requireNonNull(getMView()).onError(e);
            }
        }
    }
}
